<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增盘点计划' : '编辑盘点计划'"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="100px">
      <el-form-item label="盘点日期:" prop="date" class="block">
        <el-date-picker
          v-model="temp.date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="盘点范围:" prop="orgIds" class="block">
        <el-tree-select
          v-model="temp.orgIds"
          :clearable="false"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            multiple: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'default-expand-all': true,
            'expand-on-click-node': false,
            'highlight-current': true,
            'check-strictly': true,
            props:{
              value:'orgId',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
        />
      </el-form-item>
      <el-form-item prop="remark" class="block">
        <label slot="label">备注:</label>
        <el-input v-model="temp.remark" type="textarea" :rows="3" maxlength="255" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addInventoryPlan, queryInventoryPlanDetail, updateInventoryPlan } from '@/api'
  import { mapGetters } from 'vuex'
  import getTreeData from '@/utils/jsonTree'
  import moment from 'dayjs'

  export default {
    name: 'add',
    data() {
      return {
        visible: false,
        type: 'add',
        treeData: [],
        temp: {
          date: [],
          orgIds: [],
          remark: ''
        },
        rules: {
          date: [{ required: true, message: '请选择盘点日期', trigger: 'change' }],
          orgIds: [{ required: true, message: '请选择盘点范围', trigger: 'change' }]
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    created() {
      this.getTreeData()
    },
    methods: {
      open(temp) {
        this.getDetailData(temp.planCode)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.type = 'edit'
          }
        })
      },
      async getDetailData(planCode) {
        const res = await queryInventoryPlanDetail({ planCode })
        this.temp = res.data
        this.temp.orgIds = res.data.checkPlanOrgList.map(item => item.orgId)
        this.temp.date = [moment(res.data.startTime), moment(res.data.endTime)]
      },
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.name,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            startTime: moment(this.temp.date[0]).format(),
            endTime: moment(this.temp.date[1]).format(),
            orgIds: this.temp.orgIds.join(','),
            remark: this.temp.remark
          }
          if (this.type === 'edit') params.planCode = this.temp.planCode
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        await addInventoryPlan(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await updateInventoryPlan(params)
        this.resolve(this.temp)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          date: [],
          orgIds: [],
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
